package cn.bonoon.kernel.menus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import cn.bonoon.kernel.VisibleScope;
import cn.bonoon.kernel.util.MD5Util;
import cn.bonoon.kernel.util.StringHelper;

public abstract class ItemInfo extends AbstractItemInfo{

//	public final static String TARGET_DIALOG = "dialog";
//	public final static String TARGET_SELF = "_self";
//	public final static String TARGET_AJAX = "ajax";
//	public final static String TARGET_IFRAME = "iframe";
//	public final static String SHOW_DIALOG = "showDefaultDialog";
//	public final static String LOAD_TO_WORKSPACE = "hrefLoadToWorkspace";
//	public final static String LOAD_TO_IFRAME = "hrefLoadToIframe";

	public final static String TARGET_BLANK = "_blank";
	
	protected List<MenuInfo> menus;
	
	protected ActionInfo[] actions;

	protected VisibleScope scope;
	/**
	 * <pre>
	 * 判断时的格式为二进制的格式，如：010011100011
	 * 当前位置为1，则表示这个操作的权限是有定义的，需要对它进行权限的判断处理，如果为0则表示没有定义过，
	 * 在做操作权限判断时可以认为是可操作的
	 * </pre>
	 */
	protected int actionPrivileges;
	protected String attrs;
	protected String target;
	protected String args;
	protected DesInfo des;
	private String classPath;
	
	private static final String CLASS_PATH_PREFIX = "class:";

	public void add(MenuInfo menu) {
		if (null == menus){
			menus = new ArrayList<MenuInfo>();
		}
		menu.channel = getChannel();
		menus.add(menu);
	}

	public final void setHref(String href) {
		this.href = href;
	}
	
	protected void init(IModuleContext context, IModuleInfo module, Map<String, ItemInfo> itemCached, String pid, String phref){
		String oid = pid + '-' + id;
		this.id = MD5Util.getMD5(oid);
		this.module = module;
		String sph = phref, ohref = href;
		if(StringHelper.isNotEmpty(ohref)){
			if(ohref.startsWith(CLASS_PATH_PREFIX)){
				classPath = ohref.substring(CLASS_PATH_PREFIX.length());
				ohref = context.path("/s/sys/information/!" + id + "/index.do");
				context.add(this);
			}else{
				if(ohref.startsWith("/")){
					ohref = context.path(ohref);
				}else{
					ohref = phref + '/' + ohref;
					sph = ohref;
					if(sph.endsWith(".do")){
						int le = sph.lastIndexOf('/');
						sph = sph.substring(0, le);
					}
				}
				if(ohref.endsWith(".do")){
					this.href = ohref;
				}else{
					this.href = ohref + "/!" + id + "/index.do";
				}
				context.add(ohref, this);
			}
		} else { 
			context.add(this);
		}
		
		if(null != menus){
			Collections.sort(menus);
			for(MenuInfo menu : menus){
				menu.navigation = navigation+ ">>" + menu.getName();
				menu.init(context, module, itemCached, oid, sph);
			}
		}
		
		//这里对一些初始值进行处理，以系统运行过程中就不需要再去判断，直接引用就可以
		if(null == des){ des = module.getDes(); }
		if(StringHelper.isEmpty(key)){ key = oid; }
		if(StringHelper.isEmpty(title)){ title = name; }
		if(StringHelper.isEmpty(attrs)){ attrs = ""; }
		if(TARGET_BLANK.equalsIgnoreCase(target)){ attrs += " target='_blank'"; }
		
		itemCached.put(id, this);
	}
	
	public String getClassPath() {
		return classPath;
	}
	
	@Override
	protected void __attrs(StringBuilder a) {
		a.append(attrs);
	}
	
	public String getAttrs() {
		return attrs;
	}

	public void setAttrs(String attrs) {
		this.attrs = attrs;
	}

	public String getArgs() {
		return args;
	}

	public void setArgs(String args) {
		this.args = args;
	}

	public DesInfo getDes() {
		return des;
	}

	public void setDes(DesInfo des) {
		this.des = des;
	}

	public final List<MenuInfo> getMenus() {
		return menus;
	}

	public final String getTarget() {
		return target;
	}

	public final void setTarget(String target) {
		this.target = target;
	}

	public ActionInfo[] getActions() {
		return actions;
	}

	public void setActions(ActionInfo[] actions) {
		this.actions = actions;
	}

	public int getActionPrivileges() {
		return actionPrivileges;
	}

	public void setActionPrivileges(int actionPrivileges) {
		this.actionPrivileges = actionPrivileges;
	}
	
	public VisibleScope getScope() {
		return scope;
	}
	
	public void setScope(VisibleScope scope) {
		this.scope = scope;
	}
}
